/**
 * Created by zhangqingxi on 2017-03-31.
 */





$(function () {


    var url=modelPath+'findByParam.shtml';
    loadDataList(url);

    //高级查询
    initAdvancedSearch();

    //查询按钮事件
    initSearchClick();

    //重置参数
    initResetClick();

    //添加按钮事件
    $('#btn_solidWaste_apply_add').on('click',function () {

        openLayer("添加固废申请",modelPath+"add.shtml");

    });

    //编辑按钮事件
    $('#btn_solidWaste_apply_edit').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要修改的行！");
            return
        }
        if(checkedRows[0].auditState !== 0){
            layer.alert("当前数据已在流程当中！");
            return
        }
        var id=checkedRows[0].id;
        openLayer("编辑固废申请",modelPath+"edit.shtml?id="+id);
    });

    //删除按钮事件
    $('#btn_solidWaste_apply_delete').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要删除的行！");
            return
        }
        if(checkedRows[0].auditState !== 0){
            layer.alert("当前数据已在流程当中！");
            return
        }

        var id=checkedRows[0].id;
        initDeleteClick(modelPath,id);

    });

    //提交按钮事件
    $('#btn_solidWaste_apply_submit').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要提交的行！");
            return
        }
        if(checkedRows[0].auditState !== 0){
            layer.alert("当前数据已在流程当中！");
            return
        }

        var id=checkedRows[0].id;
        var confirmIndex=layer.confirm('是否确定提交审批？', function () {
            layer.close(confirmIndex);
            /***
             * 选择处理人
             */
            WindowManager.openLayer({
                type: 2,
                title: '选择下一环节处理人',
                area: ['370px', '180px'],
                content: ROOT_PATH + '/workflow/flowManagement/toFlowNextUserSelectPage.shtml',
                data:{id:'ff',name:'我是名称'},
                callBack:function(data){//窗口关闭后的回调函数
                    //得到选择的人员
                    if(data==null||data===""){
                        layer.alert("请选择处理人!");
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: modelPath+'editSubmit.shtml',
                        data: {id: id,nextUserId:data,auditState:1},
                        dataType: "json",
                        success: function (data) {
                            if(data.success){
                                layer.msg(data.message,{time:1000,icon:1},function () {
                                    $('#dataList').datagrid('reload');
                                });
                            }else{
                                layer.msg(data.message,{time:1000,icon:2});
                            }
                        }
                    });
                }
            });
        });
    });

});

/**
 * 加载表格
 *
 */
function  loadDataList(url) {

    $('#dataList').datagrid({
        url:url,
        queryParams:{
            sort:'d.addTime',
            order:'desc'
        },
        // idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},
            {field:'solidWasteName',title:'固废名称',width:10,formatter:function (value,row,index) {
                return '<a href="javascript:void(0)" onclick="showDetails(\''+row.id+'\')" style="color: blue">'+value+'</a>'

            }},
            {field:'solidWasteTypeName',title:'固废类别',width:10},
            {field:'hazardousWasteCode',title:'危废代码',width:10},
            {field:'vulgo',title:'俗称',width:10},
            {field:'estimatedProduction',title:'预估产生量(吨)',width:10},
            {field:'addUserName',title:'登记人',width:10},
            {field:'addDeptName',title:'登记部门',width:10},
            {field:'d.addTime',title:'登记时间',width:10,sortable:true,formatter:function (value,row,index) {
                return row.addTime;
            }},
            {field:'remarks',title:'备注',width:10},
            {field:'auditState',title:'审批状态',width:10,formatter:function (value,row,index) {

                if(row.auditState===0){
                    return '未提交';
                }else {
                    var returnValue;
                    if(row.auditState===1){
                        returnValue='未审核'
                    }else if(row.auditState===2){
                        returnValue='审核中'
                    }else if(row.auditState===7){
                        returnValue='已审核'
                    }
                    return "<a onclick=\"flowMonitorByBusinessId('" + row.id + "')\" style='color:blue;'>" + returnValue + "</a>";
                }
            }}
        ]],
        toolbar: '#toolbar'
    });
}
function showDetails(id) {

    openLayer("固废申请详情",modelPath+"details.shtml?id="+id);

}